<template>
  <a-layout-header class="header">
    <div class="logo">海洋知识库</div>
    <a-menu
        v-model:selectedKeys="selectedKeys1"
        theme="dark"
        mode="horizontal"
        :style="{ lineHeight: '64px' }"
    >
      <a-menu-item key="/">
        <router-link to="/">首页</router-link>
      </a-menu-item>
      <a-menu-item key="/admin/ebook" v-if="user.id">
        <router-link to="/admin/ebook">海洋生物管理</router-link>
      </a-menu-item>
      <a-menu-item key="/admin/category" v-if="user.id">
        <router-link to="/admin/category">分类管理</router-link>
      </a-menu-item>
      <a-menu-item key="/admin/user" v-if="user.id">
        <router-link to="/admin/user">用户管理</router-link>
      </a-menu-item>
      <a-menu-item key="/about">
        <router-link to="/about">关于我们</router-link>
      </a-menu-item>

      <a-menu-item key="/login" style="margin-left: auto" v-if="user.id">
        <span>您好,{{ user.name }}</span>
      </a-menu-item>
      <a-menu-item key="/login" style="margin-left: auto" v-if="!user.id">
        <a @click="showLoginModal">登录</a>
      </a-menu-item>

      <a-popconfirm
          title="确认退出登录?"
          ok-text="是"
          cancel-text="否"
          @confirm="logout()"
      >
        <a-menu-item v-if="user.id">
          <a>退出登录</a>
        </a-menu-item>
      </a-popconfirm>
    </a-menu>

    <a-modal
        title="登录"
        v-model:visible="loginModalVisible"
        :confirm-loading="loginModalLoading"
        @ok="login"
    >
      <a-form :model="loginUser" :label-col="{ span: 6 }" :wrapper-col="{ span: 18 }">
        <a-form-item label="登录名">
          <a-input v-model:value="loginUser.loginName" placeholder="登录名"/>
        </a-form-item>
        <a-form-item label="密码">
          <a-input v-model:value="loginUser.password" type="password" placeholder="密码"/>
        </a-form-item>
        <a-form-item label="邮箱" >
          <a-input  v-model:value="email" type="email" placeholder="邮箱" style="width: 50%"/>
          <a-button  :disabled="!isShowSend" @click="putEmail" style="margin-left: 30px">
            {{isShowSend?"发送":timeNum+"秒重新发送"}}
          </a-button>
        </a-form-item>
        <a-form-item label="验证码">
          <a-input v-model:value="loginUser.code" type="text"  />
        </a-form-item>
      </a-form>
    </a-modal>

  </a-layout-header>
</template>

<script lang="ts">
import { message } from 'ant-design-vue';
import axios from 'axios';
import {computed, defineComponent, ref} from 'vue';
import store from "@/store";
import router from "@/router";

declare let hexMd5: any;
declare let KEY: any;

export default defineComponent({
  name: 'the-header',
  setup () {
    //登录后保存
    const user = computed(() => store.state.user);

    // 用来登录
    const loginUser = ref({
      loginName: "",
      password: ""
    });
    const loginModalVisible = ref(false);
    const loginModalLoading = ref(false);
    const showLoginModal = () => {
      loginModalVisible.value = true;
    };

    // 登录
    const login = () => {
      console.log("开始登录");
      loginModalLoading.value = true;
      loginUser.value.password = hexMd5(loginUser.value.password + KEY);
        axios.post('/user/userLogin', loginUser.value).then((response) => {
          loginModalLoading.value = false;
          const data = response.data;
          if (data.success) {
            loginModalVisible.value = false;
            message.success("登录成功！");

            store.commit("setUser", data.content);
          } else {
            message.error(data.message);
          }
        });
    };

    // 退出登录
    const logout = () => {
      console.log("退出登录开始");
      axios.get('/user/logout/' + user.value.token).then((response) => {
        const data = response.data;
        if (data.success) {
          message.success("退出登录成功！");
          store.commit("setUser", {});
        } else {
          message.error(data.message);
        }
      });
      router.push("/")
    };

    //1964756261@qq.com
    const email =ref();
      //  发送邮件
      const putEmail =()=>{
        //检验邮箱
        const reg = /^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$/;
        if (reg.test(email.value)) {
          console.log("校验成功", email.value);
          axios.get("/user/sendtext",{
            params:{
              email: email.value
            }
          }).then((response)=>{
            const data = response.data;
            getSetInterval();
            if (data.success){
              message.success("邮件发送成功！")
            }else {
              message.error("邮件发送失败！")
            }

          })
        }else {
          message.error("【邮箱】不符合规则");
        }
      }

    const timeNum = ref(60);
    const countDown = ref();
    const isShowSend = ref(true);


    const getSetInterval = (): void => {


      countDown.value = setInterval(() => {
        setTimeout(() => {
          if (timeNum.value > 0 && timeNum.value <= 60) {
            isShowSend.value = false;
            timeNum.value--;
          } else {
            isShowSend.value = true;
            clearInterval(countDown.value);
          }
        }, 0);
      }, 1000);
    };


    return {
      loginModalVisible,
      loginModalLoading,
      showLoginModal,
      loginUser,
      login,
      user,
      logout,
      email,
      putEmail,
      timeNum,
      countDown,
      isShowSend
    }
  }
});
</script>

<style>
.logo {
  width: 120px;
  height: 31px;
  /*background: rgba(255, 255, 255, 0.2);*/
  /*margin: 16px 28px 16px 0;*/
  float: left;
  color: white;
  font-size: 18px;
}
.login-menu {
  float: right;
  color: white;
  padding-left: 10px;
}
</style>